package amap.com.fragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import amap.com.android_path_record.LoginActivity;
import amap.com.android_path_record.R;

import amap.com.android_path_record.User.User;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;

import static android.content.Context.MODE_PRIVATE;


public class SettingFragment extends Fragment {

    private EditText edt1, edt2, edt3, edt4;
    private Button btnn;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle saveInstanceState) {
        return inflater.inflate(R.layout.fragment_setting, null);


    }


    @Override
    public void onViewCreated(View view, @android.support.annotation.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        edt1 = (EditText) view.findViewById(R.id.stp);
        edt2 = (EditText) view.findViewById(R.id.min);
        edt3 = (EditText) view.findViewById(R.id.height);
        edt4 = (EditText) view.findViewById(R.id.weight);
        btnn = (Button) view.findViewById(R.id.btnn);
        User user = BmobUser.getCurrentUser(User.class);

        edt1.setText(user.getSteps());
        edt2.setText(user.getTime());
        edt3.setText(user.getHeight());
        edt4.setText(user.getWeight());



        btnn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (BmobUser.isLogin()) {

                    final String steps = edt1.getText().toString();
                    final String time = edt2.getText().toString();
                    final String height = edt3.getText().toString();
                    final String weight = edt4.getText().toString();

                    User user = BmobUser.getCurrentUser(User.class);
                    user.setSteps(steps);
                    user.setTime(time);
                    user.setWeight(height);
                    user.setHeight(weight);
                    user.update(new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                Toast.makeText(getActivity(), "保存成功", Toast.LENGTH_SHORT).show();
                            } else {

                                Toast.makeText(getActivity(), "保存失败，请检查网络", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });



                }


            }


        });


    }


}




